/*
 * @ author madman 
 * @ use 其他设置
 */

const ProjectModel = require('../../../models/nexmo/project.model');
const Utils = require('../../utils');
class OthenrController {
    // 添加
    async add(ctx) {
        const _body = ctx.request.body
        const _params = Utils._params(_body)
        const result = await ProjectModel.create(_params);
        if (result) {
            return ctx.success({ msg: '添加成功' });
        } else {
            return ctx.error({ msg: '添加失败' });
        }
    }
    // 更新
    async update(ctx) {
        const _body = ctx.request.body
        const _params = Utils._params(_body)
        _params.updataAt = new Date()
        const result = await ProjectModel.update({ _id: _body.id }, { $set: _params });
        if (result) {
            return ctx.success({ msg: '更新成功' });
        } else {
            return ctx.error({ msg: '更新失败' });
        }
    }
    // 查询
    async query(ctx) {
        const id = ctx.query.id
        const alias = ctx.query.alias
        var options = {}
        if (alias) {
           options.alias = alias
        }
        if (id) {
           options._id = id
        }
        console.log(options)
        let result = await ProjectModel.findOne(options);
        if (result) {
            return ctx.success({ data: result, msg: '查询成功' });
        } else {
            return ctx.error({ msg: '查询失败' });
        }
    }
    // 删除
    async delete(ctx) {
        const _id = ctx.query.id
        const result = await ProjectModel.deleteOne({ _id: _id });
        if (result) {
            return ctx.success({ msg: '删除成功' });
        } else {
            return ctx.error({ msg: '删除失败' });
        }
    }
    /*
     * 批量删除方法
     */
    async batchdelete(ctx, name) {
        const arrids = ctx.request.body.arrids
        let result = await ProjectModel.remove({ _id: { $in: arrids } })
        if (result) {
            return ctx.success({ msg: '删除成功' });
        } else {
            return ctx.error({ msg: '删除失败' });
        }
    }
    // 获取带分页列表
    async list(ctx) {
        const options = {
            name: 'nexmo/project',
            type: 'page',
            findField: {}
        }
        let result = await Utils.getPageList(ctx, options)
        if (result) {
            return ctx.success({
                data: result,
                msg: '查询成功'
            });
        }
    }

}

export default new OthenrController;